package cn.lbd.arithmetic.sort;

import org.junit.Test;

import java.util.Arrays;
import java.util.Scanner;

/*
从arr[0]到arr[n-1]中选取最小值，与arr[0]交换
从arr[1]到arr[n-1]中选取最小值，与arr[1]交换..........
**/
public class SelectSort {
    private static void sort(int[] array) {
        for (int i = 0; i < array.length - 1; i++) {
            boolean flag = false;
            int minNum = array[i];
            int index = i;
            for (int j = i; j < array.length - 1  ; j++) {
                if(minNum > array[j+1]){
                    minNum=array[j+1];
                    index = j+1;
                    flag =true;
                }
            }
            if (!flag){
                break;
            }
            int temp=array[i];
            array[i]=minNum;
            array[index] = temp;
            //System.out.println("第"+(i+1)+"轮排序："+Arrays.toString(array));
        }
    }


    public static void main(String[] args) {
        int[] array={17,3,25,14,20,9};
        System.out.println("排序前"+Arrays.toString(array));
        sort(array);
        System.out.println("排序后"+Arrays.toString(array));
        while (true) {
            Scanner scanner = new Scanner(System.in);
            int i = scanner.nextInt();
            boolean flag = false;
            for (int i1 = 0; i1 < array.length; i1++) {
                if (i == array[i1]){
                    flag = true;
                }
            }
            if (flag){
                System.out.println("元素"+i+"重复");
            }else {
                System.out.println("元素"+i+"不重复");
            }
        }
    }


    //80000数据1.738秒
    @Test
    public void batchTest(){
        int[] arr = new int[80000];
        for(int i =0; i < 80000;i++) {
            arr[i] = (int)(Math.random() * 8000000);
        }

        long start = System.currentTimeMillis();

        sort(arr);

        long end = System.currentTimeMillis();

        System.out.println((double)(end-start)/1000);
    }
}
